package MuleBackend;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.Border;


public class AuctionPanel extends JPanel{
	
	TimeClass tc = new TimeClass();	
	Timer timer = new Timer(1000, tc);
	static int timeAssigned;
	
	private JLabel titleLabel;
//	private JLabel[] priceLabel;
	private JLabel[] goodsLabel;
	private JLabel[] storeLabel;
//	private JLabel imageLabel;
	private boolean landAuction, goodAuction;
	
	private int highestBid, winner;
	
	private int[] bid;
	
	private Land landForSale;
	
	Border raisedborder, lowerborder;
	
	public AuctionPanel(){
		setLayout(null);
		landAuction = false;
		goodAuction = true;
//		priceLabel = new JLabel[4];
		goodsLabel = new JLabel[4];
		storeLabel = new JLabel[4];
		bid = new int[4];
		landForSale = null;
		buildGUI();
	}
	
	public void buildGUI(){
		
		titleLabel = new JLabel("Auction",JLabel.CENTER);
		titleLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
		titleLabel.setBounds(300,0,300,30);
		
//		priceLabel[0] = new JLabel("Buy");
//		priceLabel[0].setFont(new java.awt.Font("Tahoma", 0, 18));
//		priceLabel[0].setBounds(200,400,50,20);
//		priceLabel[1] = new JLabel("Buy");
//		priceLabel[1].setFont(new java.awt.Font("Tahoma", 0, 18));
//		priceLabel[1].setBounds(350,400,50,20);
//		priceLabel[2] = new JLabel("Buy");
//		priceLabel[2].setFont(new java.awt.Font("Tahoma", 0, 18));
//		priceLabel[2].setBounds(500,400,50,20);
//		priceLabel[3] = new JLabel("Buy");
//		priceLabel[3].setFont(new java.awt.Font("Tahoma", 0, 18));
//		priceLabel[3].setBounds(650,400,50,20);
		
		raisedborder = BorderFactory.createRaisedBevelBorder();
		lowerborder = BorderFactory.createLoweredBevelBorder();
		Border compound = BorderFactory.createCompoundBorder(raisedborder, lowerborder);
		goodsLabel[0] = new JLabel("0",JLabel.CENTER);
		goodsLabel[0].setBorder(compound);
		goodsLabel[0].setFont(new java.awt.Font("Tahoma", 0, 18));
		goodsLabel[0].setBounds(200,450,50,40);
		goodsLabel[1] = new JLabel("0",JLabel.CENTER);
		goodsLabel[1].setBorder(compound);
		goodsLabel[1].setFont(new java.awt.Font("Tahoma", 0, 18));
		goodsLabel[1].setBounds(350,450,50,40);
		goodsLabel[2] = new JLabel("0",JLabel.CENTER);
		goodsLabel[2].setBorder(compound);
		goodsLabel[2].setFont(new java.awt.Font("Tahoma", 0, 18));
		goodsLabel[2].setBounds(500,450,50,40);
		goodsLabel[3] = new JLabel("0",JLabel.CENTER);
		goodsLabel[3].setBorder(compound);
		goodsLabel[3].setFont(new java.awt.Font("Tahoma", 0, 18));
		goodsLabel[3].setBounds(650,450,50,40);
		
		storeLabel[0] = new JLabel("0",JLabel.CENTER);
		storeLabel[0].setBorder(compound);
		storeLabel[0].setFont(new java.awt.Font("Tahoma", 0, 18));
		storeLabel[0].setBounds(25,40,100,25);
		storeLabel[1] = new JLabel("0",JLabel.CENTER);
		storeLabel[1].setBorder(compound);
		storeLabel[1].setFont(new java.awt.Font("Tahoma", 0, 18));
		storeLabel[1].setBounds(25,65,100,50);
		storeLabel[2] = new JLabel("0",JLabel.CENTER);
		storeLabel[2].setBorder(compound);
		storeLabel[2].setFont(new java.awt.Font("Tahoma", 0, 18));
		storeLabel[2].setBounds(25,340,100,50);
		
		
		add(titleLabel);
//		add(priceLabel[0]);
//		add(priceLabel[1]);
//		add(priceLabel[2]);
//		add(priceLabel[3]);
		add(goodsLabel[0]);
		add(goodsLabel[1]);
		add(goodsLabel[2]);
		add(goodsLabel[3]);
		add(storeLabel[0]);
		add(storeLabel[1]);
		add(storeLabel[2]);
		
	}
	
	public void paint(Graphics g){
		super.paint(g);
		g.setColor(Color.gray);
		g.drawLine(150,100,150,380);
		g.drawLine(300,100,300,380);
		g.drawLine(450,100,450,380);
		g.drawLine(600,100,600,380);
		g.drawLine(750,100,750,380);
		
		g.setColor(Color.black);
		Graphics2D g2d = (Graphics2D) g;
		g2d.setStroke (new BasicStroke(1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1f, new float[] {2f}, 0f) );
		g.drawLine(150, 380, 750, 380);
		if (landAuction){
			g.drawLine(150, 380, 750, 380);
		}
		if (goodAuction){
			g.drawLine(150, 102, 750, 102);
			g.drawLine(150, 380, 750, 380);
		}
	}
	
	public void landAuction(){
		landAuction = true;
		goodAuction = false;
		titleLabel.setText("Land Auction");

		for (int i=0; i<bid.length;i++){
			bid[i] = 0;
		}
		highestBid = -1; winner = -1;
		
		for (int i=0; i<4;i++){
			if (i<GameDriver.game.getPlayerList().size()){
				goodsLabel[i].setText("");
			}else{
				goodsLabel[i].setVisible(false);
			}
		}
		storeLabel[0].setText("");
		storeLabel[1].setText("");
		storeLabel[2].setText("$"+landForSale.getPrice()*2);
//		timeAssigned = 4; //preferably multiple of 4
//		tc.setCounter(timeAssigned);
//		timer.start();
	}
	
	public int getBid(int playerIDNumber){
		return bid[playerIDNumber];
	}
	
	public void setWinner(){
//		GameDriver.game.getBoardGUI().colorLand(landForSale, GameDriver.game.getPlayerList().get(winner));
		if(winner == -1 || highestBid < landForSale.getPrice()*2){			
			GameDriver.game.getBoardGUI().colorLand(landForSale, null);
		}
		else{
			System.out.println("WINNER of land auction is " + winner);
			GameDriver.game.getPlayerList().get(winner).addLand(landForSale);
			GameDriver.game.getPlayerList().get(winner).deductMoney(highestBid);
			landForSale.setOwner(GameDriver.game.getPlayerList().get(winner));
			GameDriver.game.getBoardGUI().colorLand(landForSale, GameDriver.game.getPlayerList().get(winner));
		}
//		GameDriver.game.getStages().startStage(GameDriver.game.getStages().getCurrentStage() + 1);
	}
	
	public class TimeClass implements ActionListener {
		int counter;

		/**
		 * constructor for the TimeClass
		 * @param count the length of time for the timer?
		 */
		public TimeClass() {
			counter = 0;
		}
		public void setCounter(int counter){
			this.counter=counter;
		}

		/**
		 * action to perform when timer hits 0?
		 * deducts money from winning player
		 * adds land to winning player
		 * colors land to winning player's color
		 * calls on the next stage
		 */
		public void actionPerformed(ActionEvent tc) {
			counter--;
			if (counter == 0) {
				GameDriver.game.getBoardGUI().colorLand(landForSale, GameDriver.game.getPlayerList().get(winner));
				if(winner == -1 && highestBid < landForSale.getPrice()*2){					
				}
				else{
					System.out.println("WINNER of land auction is " + winner);
					GameDriver.game.getPlayerList().get(winner).addLand(landForSale);
					GameDriver.game.getPlayerList().get(winner).deductMoney(highestBid);
					landForSale.setOwner(GameDriver.game.getPlayerList().get(winner));
				}
				GameDriver.game.getStages().startStage(GameDriver.game.getStages().getCurrentStage() + 1);
			}
			
		}
	}
	
	public boolean isPlayerLandValid(int yLocation , int num,int playerIDNumber){
		if (num == 1){
			if (bid[playerIDNumber] < landForSale.getPrice()*2 && GameDriver.game.getPlayerList().get(playerIDNumber).hasMoney(landForSale.getPrice()*2)){
				bid[playerIDNumber] = landForSale.getPrice()*2;
			}else if (GameDriver.game.getPlayerList().get(playerIDNumber).hasMoney(bid[playerIDNumber]+10)){
				bid[playerIDNumber] = bid[playerIDNumber]+10;
			}
			if (bid[playerIDNumber]>highestBid){
				highestBid = bid[playerIDNumber];
				winner = playerIDNumber;
			}
			if (yLocation-10>=160)return true;
			else return false;
		}
		if (num == 2){
			if (bid[playerIDNumber]-10 < landForSale.getPrice()*2){
				bid[playerIDNumber] = 0;
			}else{
				bid[playerIDNumber] = bid[playerIDNumber]-10;
			}
			if (bid[winner]!=highestBid){
				highestBid = bid[playerIDNumber];
			}
			if (yLocation+10<=400 && bid[playerIDNumber]<=(landForSale.getPrice()*2+240))return true;
			else return false;
		}
		return false;
	}
	
	public void startLandAuction(Land landForSale){
		this.landForSale = landForSale;
		landAuction();
	}
	
	public Land getLandForSale(){
		return landForSale;
	}
	
	public void goodAuction(String goods){
		landAuction = false;
		goodAuction = true;
		titleLabel.setText(goods+" Auction");

		for (int i=0; i<bid.length;i++){
			bid[i] = 0;
		}
		highestBid = -1; winner = -1;
		
		for (int i=0; i<4;i++){
			if (i<GameDriver.game.getPlayerList().size()){
//				priceLabel[i].setText("Buy");
				goodsLabel[i].setText("");
			}else{
//				priceLabel[i].setVisible(false);
				goodsLabel[i].setVisible(false);
			}
		}
		
		storeLabel[0].setText("0");
		storeLabel[1].setText("Max");
		storeLabel[2].setText("Min");
	}
	
}
